// C Source File
// Created 11/15/2007; 2:29:01 PM

#define INSTALL

#include "Building.h"
#include "Terrain.h"
#include "Unit.h"
#include "Technology.h"

// METHODS
void CreateCivData();

// DATA
char civ_names[11][14] = {"America", "England", "France", "Russia", "China", "Japan", "Aztec", "Rome", "Greece", "Arabs", "Vikings"};
char city_names[11][15][16] = 
		{{"Washington", "New York", "Boston", "Philadelphia", "Atlanta", "Chicago", "San Francisco", "Buffalo", "St. Louis", "Detroit", "New Orleans", "Baltimore", "Denver", "Cincinnati", "Dallas"},
		 {"London", "York", "Nottingham", "Hastings", "Canterbury", "Coventry", "Warwick", "Newcastle", "Oxford", "Liverpool", "Dover", "Brighton", "Norwich", "Leeds", "Reading"},
		 {"Paris", "Orleans", "Lyons", "Rheims", "Tours", "Marseilles", "Chartres", "Avignon", "Besancon", "Rouen", "Grenoble", "Dijon", "Amiens", "Cherbourg", "Poitiers"},
		 {"Moscow", "St. Petersburg", "Kiev", "Minsk", "Smolensk", "Odessa", "Sevastopol", "Tblisi", "Sverdlovsk", "Yakutsk", "Vladivostok", "Novgorod", "Krasnoyarsk", "Riga", "Rostov"},
		 {"Beijing", "Shanghai", "Canton", "Nanking", "Tsingtao", "Xinjian", "Chengdu", "Hangchow", "Tientsin", "Tatung", "Macao", "Anyang", "Shantung", "Chinan", "Kaifeng"},
		 {"Kyoto", "Osaka", "Edo", "Satsuma", "Kagoshima", "Nara", "Nagoya", "Izumo", "Nagasaki", "Yokohama", "Shimonoseki", "Matsuyama", "Sapporo", "Hakodate", "Ise"},
		 {"Tenochtitlan", "Teotihuacan", "Tlatelolco", "Texcoco", "Tlaxcala", "Calixtlahuaca", "Xochicalco", "Tlacopan", "Atzcapotzalco", "Tzintzuntzen", "Malinalco", "Tula", "Tamuin", "Teayo", "Cempoala"},
		 {"Rome", "Veii", "Antium", "Cumae", "Neapolis", "Pompeii", "Pisae", "Ravenna", "Hispalis", "Viroconium", "Lugdunum", "Lutetia", "Byzantium", "Brundisium", "Syracuse"},
		 {"Athens", "Sparta", "Thermopylae", "Corinth", "Delphi", "Pharsalos", "Knossos", "Argos", "Mycenae", "Herakleia", "Ephesos", "Thessalonica", "Rhodes", "Eretria", "Troy"},
		 {"Baghdad", "Mecca", "Samarra", "Damascus", "Tiflis", "Tangier", "Raqqa", "Beirut", "Basra", "Medina", "Riyadh", "Mosul", "Beirut", "Algiers", "Tunis"},
		 {"Trondheim", "Kaupang", "Uppsala", "Hladir", "Aarhus", "Viborg", "Roskilde", "The Udal", "Lindholm", "Jorvik", "Westness", "Jarrow", "Skara", "Ravning Enge", "Birka"}};
unsigned short int unitpic[12][9] = 
		{{0x1C00,0x1C00,0x1C00,0x4900,0x3E00,0x0800,0x1C00,0x1400,0x2200},  //settler
		 {0x3800,0x3800,0x3800,0x9380,0x7F80,0x1380,0x3900,0x2800,0x4400},  //warrior
		 {0x3900,0x3B80,0x3900,0x9300,0x7D00,0x1100,0x3900,0x2900,0x4500},  //pikeman
		 {0x0000,0x1800,0x1B80,0x1380,0x1200,0x7E00,0x7E00,0x4200,0x4200},  //horseman
		 {0x3800,0x3A00,0x3B00,0x9280,0x7E80,0x1280,0x3B00,0x2A00,0x4400},  //archer
		 {0x3800,0x3A00,0x3A00,0x9200,0x7F00,0x1200,0x2800,0x2800,0x4400},  //swordman
		 {0x3000,0x3000,0x3000,0x2700,0x3C00,0x2400,0x3000,0x2800,0x2800},  //rifleman
		 {0x0000,0x6000,0x7000,0x0800,0x0400,0x0200,0x7F00,0x6300,0x6300},  //catapult
		 {0x0000,0x0000,0x0080,0x0300,0x3C00,0x3800,0xFF80,0xFF00,0x7E00},  //tank
		 {0xF780,0xF380,0xF180,0xF080,0xF080,0xF780,0x0000,0x8080,0xC180},  //galley
		 {0x0E00,0x0E00,0xCE00,0xFF80,0xFF80,0xFF80,0xCE00,0x0E00,0x0E00},  //bomber
		 {0x0C00,0xCC00,0xCE00,0xFF00,0xFF00,0xCE00,0xCC00,0x0C00,0x0000}}; //fighter
unsigned short int backpic[8][9] = 
	 {{0xFFC0,0xFFC0,0xD5C0,0xABC0,0xFFC0,0xEAC0,0xD5C0,0xFFC0,0xFFC0},  //water
  	{0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040},  //desert
		{0x0040,0x5540,0x5540,0x0040,0x2A40,0x2A40,0x0040,0x5540,0x5540},  //grassland
		{0x4440,0x5540,0x1140,0x0040,0x0040,0x0040,0x4440,0x5540,0x1140},  //plains
		{0x3840,0x4440,0x8240,0x1C40,0x2240,0x4140,0x0E40,0x1140,0x20C0},  //tundra
		{0x2240,0x7740,0xFFC0,0x2240,0x0040,0x2240,0x7740,0xFFC0,0x2240},  //forest
		{0x0040,0x0040,0x0040,0x0040,0x0040,0x3C40,0x6640,0xC340,0x81C0},  //hills
		{0x0840,0x1C40,0x1440,0x3640,0x2240,0x6340,0x4140,0xC1C0,0x80C0}}; //peaks

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main Function //////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void _main(void)
{
	ClrScr();
	FontSetSys(F_4x6);
	DrawStr(0,0, "Installing Civ2000 Data..", A_NORMAL);
	
	CreateCivData();
	
	DrawStr(0, 75, "Finished.", A_NORMAL);
	ngetchx();
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CreateCivData //////////////////////////////////////////////////////////////////////////////////////////////////
// creates the data for the game //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CreateCivData()
{
	// Open file for reading and check current version
	FILE *f = fopen("civdata","rb");
	if(f != NULL)
	{
		char v[7];
		
		fread(v, 7*sizeof(char),1,f);
		
		if(v[0] == 'v')
		{
			int _v = ((int)(v[1] - '0'))*100 + ((int)(v[3] - '0'))*10 + ((int)(v[5] - '0'));
			
			if(_v > VERSION)
			{
				HANDLE dlg = H_NULL;
				if((dlg = DialogNewSimple(120,35)) != H_NULL)
				{
			   	// Format the dialog box
			    DialogAddTitle(dlg,"Confirm",BT_OK,BT_CANCEL);
			    char str[25];
			    sprintf(str, "Revert to %s?", version);
			    DialogAddText(dlg,30,15,str);
			    
			    int key = 0;
			    while (key != KEY_ENTER && key != KEY_ESC) { key = DialogDo(dlg,CENTER,CENTER,NULL,NULL); }
			    
					FontSetSys(F_4x6);
			    if(key != KEY_ENTER) return;
				}
				else return;
			}
		}
		
		fclose(f);
	}	
	
	// Open file for writing
	if ((f = fopen("civdata","wb")) == NULL) 
		return;
		
	char str[25];
	sprintf(str, "civdata %s..", version);
	DrawStr(5, 10, str, A_NORMAL);
		
	// Populate all the arrays
	CreateUnits();
	CreateTerrain();
	CreateTBonus();
	CreateBuildings();
	CreateTechnology();
		
	// Write structure data to the file
	fwrite(version, 7*sizeof(char),1,f);
	
	DrawStr(10, 17, "Names", A_NORMAL);
	fwrite(civ_names, 11*14*sizeof(char),1,f);
	fwrite(city_names, 11*15*16*sizeof(char),1,f);
	
	DrawStr(10, 24, "Pictures", A_NORMAL);
	fwrite(unitpic, 12*9*sizeof(unsigned short int),1,f);
	fwrite(backpic, 8*9*sizeof(unsigned short int),1,f);
	
	DrawStr(10, 31, "Unit data", A_NORMAL);
	fwrite(_units, 19*sizeof(Unit),1,f);
	
	DrawStr(10, 38, "Terrain data", A_NORMAL);
	fwrite(terrain, 8*sizeof(Terrain),1,f);
	fwrite(tBonus, 21*sizeof(TBonus),1,f);
	
	DrawStr(10, 45, "Building data & strings", A_NORMAL);
	fwrite(buildings, 67*sizeof(Building),1,f);
	fwrite(buildngsDesc, 67*4*12*sizeof(char),1,f);
	
	DrawStr(10, 52, "Technology Data", A_NORMAL);
	fwrite(techs, 90*sizeof(Technology),1,f);

	// Append the file ID tag
	fputc(0,f);
	fputs("civ",f);
	fputc(0,f);
	fputc(OTH_TAG,f);
	
	// Close the file
	fclose(f);
}
